<include file="header" />
<body>
	<div class="wraper">
		<div class="load">
			<img class="load-bee" src="/assets/base/repair/images/load-bee.png"/>
		</div>
	    <div class="top">
	        <p>
	        	<a href="javascript:history.back(-1)" class="iconfont icon-zuo fl-left"></a>
	        	我要报修
	        	<a href="{BEESCRM::U('base/repair/orderlist',array('repairId'=>$repairId))}" class="iconfont icon-shouye fl-right"></a>
	        </p>
	    </div>
	   <div class="upload-img clearfix">
		   <div class="imgbox fl-left"><img src="{BEESCRM:$data['img']['url']}" id="picture" style="width:100px;height:100px;"/></div>
		   <div class="add-img fl-left" >
			   <input class="file-input" type="file" accept="image/*" name="upload-file" id="fileupload_input"/>
			   <span class="iconfont icon-tianjia tianjia"></span>
		   </div>
    	</div>
    <div>
        <p class="form-box">
            <textarea class="textarea" placeholder="请用简短的话描述要报修的情况" id="content">{BEESCRM:$data.content}</textarea>
        </p>
        <p class="form-box">
            <select class="select" id="project_id">
            	<option value="0">请选择报修项目</option>
            	<volist name="project" id="vo">
            		<option value="{BEESCRM:$vo.project_id}" <if condition="$data['project_id'] eq $vo['project_id']">selected="selected"</if>>
            			{BEESCRM:$vo['project_name']}
            		</option>
            	</volist>
            </select>
            <span class="triangle"></span>
        </p>
        <p class="form-box" id="display_project" <if condition="$data['project_child_id'] eq null"> style="display:none"</if>>
            <select class="select" id="project_child_id" >
            	<option value="0">请选择分类</option>
            	<if condition="$data['project_child_id'] gt 0">
            	<volist name="childProject" id="list">
            	<option value="{BEESCRM:$list['project_id']}" <if condition="$data['project_child_id'] eq $list['project_id']">selected="selected"</if>>{BEESCRM:$list['project_name']}</option>
            	</volist>
            	</if>
            </select>
            <span class="triangle"></span>
        </p>
    </div>
    <div class="message clearfix">
        <p class="title">报修地址</p>
        <div class="clearfix" style="position: relative">
            <p class="mes fl-left">
				{BEESCRM:$data['address_detail']}
            	<br/>
            	{BEESCRM:$data['real_name']}  {BEESCRM:$data['mobile']}
            </p>
            <a href="javascript:void(0)" class="btn-change fl-right">更换地址</a>
        </div>
    </div>
    <if condition="$repairInfo['reminder']">
        <div style="padding: 15px 5px;">
            <p class="field"><span style="color: red;">*温馨提示：{BEESCRM:$repairInfo.reminder}</span></p>
        </div>
    </if>
    <div class="marginb-50"></div>
    <ul class="btn-box">
    	<input type="hidden" id="address_id" value="{BEESCRM:$data['address_id']}">
    	<input type="hidden" id="order_id" value="{BEESCRM:$data['order_id']}">
    	<input type="hidden" id="cost" value="0">
        <a href="javascript:void(0)" class="btn half addorder" >提交</a>
        <a href="javascript:history.back(-1)" class="btn half" >取消</a>
    </ul>
</div>

	<input type="hidden" name="imageNeed" id="imageNeed" value="{BEESCRM:$repairInfo['image_condition']}">
    <input type="hidden" id="is_back" value="{BEESCRM:$isBack}">

<include file="commonJS" />
<script type="text/javascript" src="/assets/bower_components/bootstrap/dist/js/bootstrap.min.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.ui.widget.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.fileupload.js"></script>
<script type="text/javascript" src="/assets/bower_components/upload/jquery.iframe-transport.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#fileupload_input").fileupload({
			url:"/base/common/upload",//文件上传地址，当然也可以直接写在input的data-url属性内
			beforeSend:function(){
				$('.load').show();
			},
			done:function(e,result){
				//done方法就是上传完毕的回调函数，其他回调函数可以自行查看api
				//注意result要和jquery的ajax的data参数区分，这个对象包含了整个请求信息
				//返回的数据在result.result中，假设我们服务器返回了一个json对象
				var data=$.parseJSON(result.result);
				if(data.status=='S'){
					$('#picture').attr('src',data.url);
					setTimeout($('.load').hide(),3000);
				}else{
					$.alert('error',data.message);
					setTimeout($('.load').hide(),3000);
					return false;
				}
			}
		});
		changeData();
		$("#project_id").change(function () {
			changeData();
		});

		function changeData(){
			var id = $("#project_id").val();
			var name = getParams(id);
			var options = [];
			if(Number(name)==0){
				$('#display_project').hide();
				option = '<option value="0">请选择子项目</option>';
				options.push(option);
			}else{
				$('#display_project').show();
				option = '<option value="0">请选择子项目</option>';
				options.push(option);
				$.each(name, function (index, node) {
					var child_project_id = parseInt("{BEESCRM:$data.project_child_id}");
					if(child_project_id == node.project_id){
						option = '<option selected="selected" value="' + node.project_id+'">' + node.project_name + '</option>';
					}else{
						option = '<option value="' + node.project_id+'">' + node.project_name + '</option>';
					}
					
					options.push(option);
				});
			}
			$("#project_child_id").html(options.join(''));
		}

		function getParams(id, colname, table_name) {
			id = id || null;
			if(id == 0) {
				return false;
			}
			var result = null;
			$.ajax({
				url: "{BEESCRM::U('base/Repair/getChildProject',array('repairId'=>$repairId))}",
				type: "post",
				dataType: 'json',
				async: false,
				data: {
					id: id/*,
					colname: colname,
					table_name: table_name*/
				},
				success: function (data) {
					if (data.status == 0) {
						result = data.result;
					}else{
						$('#cost').val(data.cost);//赋值维修金额
					}
				}
			});
			return result;
		}

        // 提交订单
		$('.addorder,.btn-change').on('click',function(){
			var data = {};
			data.content  = $('#content').val();
			data.project_id=$('#project_id').val();
			
			data.order_id=$('#order_id').val();
			data.image=$('#picture').attr('src');
			data.project_child_id=$('#project_child_id').val();

            // 是否返修订单
            data.is_back = $('#is_back').val();

			var cost = 0;
			if(data.project_child_id>0 && data.project_child_id!=undefined && data.project_child_id!= null){
				$.ajax({
					url:"{BEESCRM::U('Base/Repair/getCost',array('repairId'=>$repairId))}",
					async:false,
					type:"post",
					data:{'project_id':data.project_child_id},
					dataType:"json",
					success:function(res){
						cost = res.cost;
					}
				})
			}else{
				cost = $('#cost').val();
			}
			var url;
					//判断来源
					if($(this).hasClass('btn-change')){
						data.type='switchAddress';
					}else{
						data.type='addOrder';
					}
					data.address_id=$('#address_id').val();
					//参数验证
					if(data.type == 'addOrder'){

						if($("#imageNeed").val() == 1) {
							if(!data.image) {
								$.alert('error','请上传相关图片');return false;
							}
						}
						if(!data.content){
							$.alert('error','请填写报修内容');return false;
						}
						if(data.project_id=='0'){
							$.alert('error','请选择报修项目');return false;
						}
                        if ($('#display_project').css('display') == 'block' && data.project_child_id == '0') {
                            $.alert('error','请选择子项目');return false;
                        }

						if(!data.address_id){
							$.alert('error','请选择您的地址');return false;
						}

						var community_name = "{BEESCRM:$data.community_name}";
						var userType = "{BEESCRM:$user.type}";
						if(!community_name && userType == 0){
							$.alert('error','地址有误,请点击变更地址重新添加或者修改');return false;//防止无园区等信息的地址出现
						}

						if(cost>0){
							$.confirm('本次报修需要支付'+cost+'元维修费,确定提交订单吗',function(){
								data.order_amount = cost;
								addOrder(data);
							});
						}else{
							addOrder(data);
						}
					}else{
						addOrder(data);
					}
			
		});
	});	

	function addOrder(data){
		//订单存储
		$('.load').show();
		$.ajax({
			type:'post',
			dataType:'json',
			data:data,
			url:"{BEESCRM::U('base/Repair/addOrder',array('repairId'=>$repairId))}",
			success:function(res){
				console.log(res);
				if(res.status == 'S'){
					if(data.type=='addOrder'){
						url="{BEESCRM::U('base/Repair/orderList',array('repairId'=>$repairId))}";
					}else{
						url="{BEESCRM::U('Base/address/address',array('repairId'=>$repairId,'type'=>'1002','is_repair'=>1))}/order_id/"+res.data;
					}
					location.href=url;
					$('.load').hide();
				}else{
					$.alert('error',res.message);
					$('.load').hide();
					return false;
				}
			},
			error:function(res){
				$.alert('error','通讯错误');
				$('.load').hide();
				return false;
			}
		});
	}
</script>
</body>
</html>
